/*
Copyright (C) 2011 Salil Bhagurkar

This file is part of illusion

illusion is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

illusion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with illusion.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <klib/memory.h>

void *memset(void *start, u8 value, uint_t count)
{
	u32 value_l = value | (value << 8) | (value << 16) | (value << 24);
	u32 *start_l = (u32 *)start;
	uint_t count_l = count / sizeof(u32);
	uint_t final = count - count_l * sizeof(u32);
	uint_t i;
	for(i = 0; i < count_l; i++) {
		*start_l++ = value_l;
	}
	char *start_c = (char *)start_l;
	for(i = 0; i < final; i++) {
		*start_c++ = value;
	}
	return start;
}


void *memcpy(void *dst, void *src, uint_t count)
{
	u32 *dst_l = (u32 *)dst;
	u32 *src_l = (u32 *)src;
	uint_t count_l = count / sizeof(u32);
	uint_t final = count - count_l * sizeof(u32);
	uint_t i;
	for(i = 0; i < count_l; i++) {
		*dst_l++ = *src_l++;
	}
	char *dst_c = (char *)dst_l;
	char *src_c = (char *)src_l;
	for(i = 0; i < final; i++) {
		*dst_c++ = *src_c++;
	}
	return dst;
}

bool memeq(void *m1, void *m2, uint_t count)
{
	u32 *m1_l = (u32 *)m1;
	u32 *m2_l = (u32 *)m2;
	uint_t count_l = count / sizeof(u32);
	uint_t final = count - count_l * sizeof(u32);
	uint_t i;
	for(i = 0; i < count_l; i++) {
		if(*(m1_l) != *(m2_l))
			return false;
		m1_l++;
		m2_l++;
	}
	char *m1_c = (char *)m1_l;
	char *m2_c = (char *)m2_l;
	for(i = 0; i < final; i++) {
		if(*(m1_c) != *(m2_c))
			return false;
		m1_c++;
		m2_c++;
	}
	return true;
}
